﻿using System;
using System.IO;
using log4net;
using log4net.Config;
using log4net.Appender;

namespace ExpenseSystem.Common
{
    public static class LogHelper
    {
        static ILog sysLogger;
        static ILog audLogger;
        static bool inited;
        public static bool Init()
        {
            if (inited)
                return true;
            string config_path = Variable.EXEC_PATH + "config\\log4net.xml";
            if(!File.Exists(config_path))
            {
                Variable.INNER_ERROR = "系统启动失败，配置有误。";
                return false;
            }
            try
            {
                FileInfo logFile = new FileInfo(config_path);
                XmlConfigurator.Configure(logFile);
                audLogger = LogManager.GetLogger("Audit");
                sysLogger = LogManager.GetLogger("System");
                inited = true;
                return true;
            }
            catch(Exception ex)
            {
                Variable.INNER_ERROR = ex.Message;
                return false;
            }
        }
        public static void Error(string client,string message)
        {
            string msg = String.Format("client:{0},{1}",client,message);
            sysLogger.Error(msg);
        }
        public static void Info(string client, string message)
        {
            string msg = String.Format("client:{0},{1}", client, message);
            sysLogger.Info(msg);
        }
        public static void Auth(string client, string message)
        {
            string msg = String.Format("client:{0},{1}", client, message);
            audLogger.Info(msg);
        }
    }
}
